<?php
tsload(APPS_PATH.'/admin/Lib/Action/AdministratorAction.class.php');

class SalerAction extends AdministratorAction{
	
	public function _initialize(){
		$this->pageTitle['index'] = '分销申请';
		$this->pageTitle['done'] = '已处理申请';
		parent::_initialize();
	}
	//申请列表
	public function index(){
		$_REQUEST['tabHash'] = 'index';
		// 初始化用户列表管理菜单
		$this->_initSalerListAdminMenu("index");
		// 数据的格式化与listKey保持一致
		$map["status"] = 1;
		$listData = $this->_getSalerList('20', $map,"index");
		
		// 列表批量操作按钮
		//$this->pageButton[] = array('title'=>"搜索",'onclick'=>"admin.fold('search_form')");
		
		//$this->pageButton[] = array('title'=>L('PUBLIC_TRANSFER_USER_GROUP'),'onclick'=>"admin.changeUserGroup()");
		// 转移用户部门，如果需要请将下面的注释打开
		// $this->pageButton[] = array('title'=>L('PUBLIC_TRANSFER_DEPARTMENT'),'onclick'=>"admin.changeUserDepartment()");
		$this->displayList($listData);
	}
	//已处理列表
	public function done(){
		$_REQUEST['tabHash'] = 'done';
		// 初始化用户列表管理菜单
		$this->_initSalerListAdminMenu("done");
		// 数据的格式化与listKey保持一致
		$map["status"] = 1;
		$listData = $this->_getSalerList('20', $map,"done");
		
		// 列表批量操作按钮
		$this->pageButton[] = array('title'=>"搜索",'onclick'=>"admin.fold('search_form')");
		
		//$this->pageButton[] = array('title'=>L('PUBLIC_TRANSFER_USER_GROUP'),'onclick'=>"admin.changeUserGroup()");
		// 转移用户部门，如果需要请将下面的注释打开
		// $this->pageButton[] = array('title'=>L('PUBLIC_TRANSFER_DEPARTMENT'),'onclick'=>"admin.changeUserDepartment()");
		$this->displayList($listData);
	}
	public function _initSalerListAdminMenu(){
		// tab选项
		$this->pageTab[] = array('title'=>"分销申请",'tabHash'=>'index','url'=>U('admin/Saler/index'));
		$this->pageTab[] = array('title'=>"已处理申请",'tabHash'=>'done','url'=>U('admin/Saler/done'));
		
		// 搜索选项的key值
		// $this->searchKey = array('uid','uname','email','sex','department','user_group',array('ctime','ctime1'));
		$this->searchKey = array('id','uid',"uname",'applystatus',"phone",array('applytime','applytime1'));
		// 针对搜索的特殊选项
		$this->opt['status'] = array('0'=>"未处理",'1'=>"已处理");
		
		
		
		
		// 列表key值 DOACTION表示操作
		$this->pageKeyList = array('id','uid','uname','applystatus','applytime',"phone",'DOACTION');
		
		
	}
	
	private function _getSalerList($limit = 20, $map = array(),$tab) {
		// 设置列表主键
		$this->_listpk = 'id';
		// 取用户列表
		if ($tab == "index"){
			$sql = "select s.*,u.uname,u.phone from yjsaler as s left join yjuser as u on s.uid = u.uid where applystatus = 0 order by applytime desc";
			$listData["data"] = M('Saler')->query($sql);
		}elseif($tab == "done"){
			$sql = "select s.*,u.uname,u.phone from yjsaler as s left join yjuser as u on s.uid = u.uid where applystatus = 1 order by applytime desc";
			$listData["data"] = M('Saler')->query($sql);
		}
		
		
		foreach($listData['data'] as $k => $v) {
			//获取用户组信息

			$listData['data'][$k]['applytime'] = date('Y-m-d H:i:s',$listData['data'][$k]['applytime']);
			if ($listData['data'][$k]['applystatus'] == 0){
				$listData['data'][$k]['applystatus'] = "未处理";
			}elseif ($listData['data'][$k]['applystatus'] ==1){
				$listData['data'][$k]['applystatus'] = "已处理";
			}
			//$listData['data'][$k]['uid'] = M("User")->where("uid = ".$listData['data'][$k]['uid'])->select()['uname'];
			//设置操作
			$listData['data'][$k]['DOACTION'] .= $v['applystatus'] == 0 ? '<a href="javascript:void(0)" onclick="admin.doneSaler(\''.$v['id'].'\')">'.'处理'.'</a> ' : '<a href="javascript:void(0)" onclick="admin.delSaler(\''.$v['id'].'\')">'.'删除'.'</a>  ';
			
		}
		return $listData;
	}
	//处理操作
	public function doDoneSaler(){
		$id = t($_REQUEST['id']);
		$saler = M("Saler");
		$saler->applystatus = 1;
		if ($saler->where("id = $id")->save()){
			$return['status'] = 1;
			$return['data'] = "处理完成";
			}else{
				$return['status'] = 0;
				$return['data'] = "操作失败";
			}
// 		$str ="";
// 		foreach ($_REQUEST as $k =>$v){
// 			$str .= $k."+"."$v"."/";
// 		}
// 		$return['data'] = $str;
		exit(json_encode($return));
	}
	
	//删除操作
	public function doDelSaler(){
		$id = t($_REQUEST['id']);
		if (M("Saler")->where("id = $id")->delete()){
			$return['status'] = 1;
			$return['data'] = "删除完成";
		}else{
			$return['status'] = 0;
			$return['data'] = "删除失败";
		}
		exit(json_encode($return));
	}
}